package com.ruoyi.web.controller.hbs;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hbsutils.HbsUtil;
import com.ruoyi.web.service.impl.HbsDataSyncServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/hbs/hbsBusiness")
@Api(tags = "HBS业务采集接口")
public class HbsBusinessController {

    @Autowired
    private HbsUtil hbsUtil;
    @Autowired
    private HbsDataSyncServiceImpl hbsDataSyncService;

    /**
     * 获取表格列表
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-获取表格列表", notes = "获取HBS系统中的表格列表信息", tags = {"HBS业务采集接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "请求参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = R.class),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequestMapping(value = "/openapi/v1/table/list", method = RequestMethod.POST)
    @ResponseBody
    public R openapiV1TableList(@RequestBody JSONObject bodyJson) throws Exception {
        return hbsUtil.postHBSService("openapi/v1/table/list", "DHY业务链", null, bodyJson, "HBS-获取表格列表");
    }

    /**
     * 获取表格配置
     *
     * @param tableId  表格ID
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-获取表格配置", notes = "根据表格ID获取表格的详细配置信息", tags = {"HBS业务采集接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "表格ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "bodyJson", value = "请求参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = R.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequestMapping(value = "/openapi/v1/table/{tableId}", method = RequestMethod.POST)
    @ResponseBody
    public R openapiV1TableConfig(@PathVariable String tableId, @RequestBody JSONObject bodyJson) throws Exception {
        return hbsUtil.postHBSService("openapi/v1/table/" + tableId, "DHY业务链", null, bodyJson, "HBS-获取表格配置");
    }

    /**
     * 获取数据列表
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-获取数据列表", notes = "获取指定条件下的数据列表", tags = {"HBS业务采集接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "请求参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = R.class),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequestMapping(value = "/openapi/v1/item/list", method = RequestMethod.POST)
    @ResponseBody

    public R openapiV1ItemList(@RequestBody JSONObject bodyJson) throws Exception {
        return hbsUtil.postHBSService("openapi/v1/item/list", "DHY业务链", null, bodyJson, "HBS-获取数据列表");
    }

    /**
     * 批量更新或创建数据
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-批量更新或创建数据", notes = "批量更新或创建数据数据", tags = {"HBS业务采集接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "请求参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = R.class),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequestMapping(value = "/openapi/v1/items", method = RequestMethod.PUT)
    @ResponseBody
    public R openapiV1Items(@RequestBody JSONObject bodyJson) throws Exception {
        return hbsUtil.putHBSService("/openapi/v1/items", "DHY业务链", null, bodyJson, "HBS-批量创建数据");
    }




    /**
     * 同步产品档案数据
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-同步产品档案数据", notes = "从HBS系统同步产品档案数据到本地数据库", tags = {"HBS数据同步接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "同步参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "同步成功", response = R.class),
            @ApiResponse(code = 500, message = "同步失败")
    })
    @RequestMapping(value = "/sync/productArchive", method = RequestMethod.POST)
    @ResponseBody
    public R syncProductArchive(@RequestBody JSONObject bodyJson) {
        try {
            hbsDataSyncService.syncProductArchiveData(bodyJson);
            return R.ok("同步产品档案数据成功");
        } catch (Exception e) {
            return R.fail("同步产品档案数据失败: " + e.getMessage());
        }
    }

    /**
     * 同步商业档案数据
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-同步商业档案数据", notes = "从HBS系统同步商业档案数据到本地数据库", tags = {"HBS数据同步接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "同步参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "同步成功", response = R.class),
            @ApiResponse(code = 500, message = "同步失败")
    })
    @RequestMapping(value = "/sync/businessArchive", method = RequestMethod.POST)
    @ResponseBody
    public R syncBusinessArchive(@RequestBody JSONObject bodyJson) {
        try {
            hbsDataSyncService.syncBusinessArchiveData(bodyJson);
            return R.ok("同步商业档案数据成功");
        } catch (Exception e) {
            return R.fail("同步商业档案数据失败: " + e.getMessage());
        }
    }

    /**
     * 同步终端档案数据
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-同步终端档案数据", notes = "从HBS系统同步终端档案数据到本地数据库", tags = {"HBS数据同步接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "同步参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "同步成功", response = R.class),
            @ApiResponse(code = 500, message = "同步失败")
    })
    @RequestMapping(value = "/sync/terminalArchive", method = RequestMethod.POST)
    @ResponseBody
    public R syncTerminalArchive(@RequestBody JSONObject bodyJson) {
        try {
            hbsDataSyncService.syncTerminalArchiveData(bodyJson);
            return R.ok("同步终端档案数据成功");
        } catch (Exception e) {
            return R.fail("同步终端档案数据失败: " + e.getMessage());
        }
    }



    /**
     * 同步数据(采集数据同步到伙伴云)
     *
     * @param bodyJson 请求体
     * @return 响应结果
     */
    @ApiOperation(value = "HBS-采集数据同步到伙伴云", notes = " 同步数据(采集数据同步到伙伴云)", tags = {"HBS数据同步接口"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bodyJson", value = "同步参数", required = true, dataType = "JSONObject", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "同步成功", response = R.class),
            @ApiResponse(code = 500, message = "同步失败")
    })
    @RequestMapping(value = "/sync/syncDataToHby", method = RequestMethod.PUT)
    @ResponseBody
    public R syncDataToHby(@RequestBody JSONObject bodyJson) {
        try {
            hbsDataSyncService.syncDataToHby(bodyJson);
            return R.ok("采集数据同步到伙伴云成功");
        } catch (Exception e) {
            return R.fail("采集数据同步到伙伴云失败: " + e.getMessage());
        }
    }
}
